#ifndef AVALON_HAL_H_
#define AVALON_HAL_H_

#ifdef __NIOS2__

#include "io.h"
#include "hal/register.h"
#include "system.h"
#include <stdio.h>

#if USE_AVALON_DATA_CHECK
#define AVALON_DATA_CHECK_WRITE()           if (data == 0xdeadbeef) printf("av_bus_write_32() addr=0x%08lx data=0x%08lx\n", addr, data)
#define AVALON_DATA_CHECK_READ()            if (data == 0xdeadbeef) printf("av_bus_read_32() addr=0x%08lx data=0x%08lx\n", addr, data)
#else
#define AVALON_DATA_CHECK_WRITE()
#define AVALON_DATA_CHECK_READ()
#endif

#if USE_AVALON_BUS_WAIT
#define AVALON_BUS_WAIT(seq)                for(uint16_t k = 0; k < seq; k++) __asm__("nop")
#else
#define AVALON_BUS_WAIT(seq)
#endif

static inline void av_bus_write_32(unsigned long addr, unsigned long data)
{
    AVALON_DATA_CHECK_WRITE();
    IOWR_32DIRECT(WISH_BASE,addr,data);
    AVALON_BUS_WAIT(AVALON_BUS_WAIT_TIME);

}

static inline unsigned long av_bus_read_32(unsigned long addr)
{
    unsigned long data = IORD_32DIRECT(WISH_BASE,addr);
    AVALON_BUS_WAIT(AVALON_BUS_WAIT_TIME);
    AVALON_DATA_CHECK_READ();

     return data;
}

#endif

#endif /*AVALON_HAL_H_*/
